package com.example.yinhangmiaosha.controller;

import com.example.yinhangmiaosha.domain.JSONResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@Controller
//@RequestMapping("")
public class LoginController {
//    @Autowired
//    private IEmployeeService employeeService;

    @RequestMapping("/userLogin")
    @ResponseBody
    public JSONResult userLogin(String username, String password, HttpServletRequest request) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            return new JSONResult(true,"success");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return new JSONResult(false,"fail");
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("USER_IN_SESSION");

        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    @ResponseBody
    public String nopermission() {
        return "无权限！";
    }
}
